package com.huatai.bpmn.service.impl;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huatai.bpmn.commom.emnu.DicCode;
import com.huatai.bpmn.mapper.DicMapper;
import com.huatai.bpmn.entity.DicEntity;
import com.huatai.bpmn.service.DicService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 字典配置
 * </p>
 *
 * @author yxy
 * @since 2023-07-07
 */
@Service
public class DicServiceImpl extends ServiceImpl<DicMapper, DicEntity> implements DicService {

	/**
	 * 表头类型列表,表格类型id为41
	 *
	 * @return
	 */
	public List<DicEntity> list(String type) {
		LambdaQueryWrapper<DicEntity> query = new LambdaQueryWrapper<>(DicEntity.class);
		if (DicCode.parentType.getCode().equals(type)) {

			query.eq(DicEntity::getPid, DicCode.parentType.getNum());
		}

		if (DicCode.dataType.getCode().equals(type)) {

			query.eq(DicEntity::getPid, DicCode.dataType.getNum());
		}

		if (DicCode.formExternalParameterType.getCode().equals(type)) {

			query.eq(DicEntity::getPid, DicCode.formExternalParameterType.getNum());
		}
		List<DicEntity> list = this.list(query);
		return list;
	}

	/**
	 * 获取字典树形结构数据
	 * @return
	 */
	@Override
	public List<DicEntity> treeList() {
		List<DicEntity> allList = this.list();
		List<DicEntity> list = this.list(new LambdaQueryWrapper<>(DicEntity.class).eq(DicEntity::getPid, DicCode.parentType.getNum()));
		list.stream().forEach(x->{
			//父id
			String parentId = x.getId();
			//子数据
			List<DicEntity> childrenList = findChildrenList(parentId, allList);
			//List<DicEntity> childrenList = this.list(new LambdaQueryWrapper<>(DicEntity.class).eq(DicEntity::getPid, parentId));
			x.setChildren(childrenList);

			//getChildrenList(childrenList);
			getChildrenList(childrenList,allList);
		});
		return list;
	}

	@Override
	public DicEntity getCode(String jsonKey) {
		return this.getOne(Wrappers.<DicEntity>lambdaQuery().eq(DicEntity::getCode,jsonKey));
	}
	/**
	 * 根据父id查询数据列表
	 * @param id
	 * @return
	 */
	@Override
	public List<JSONObject> findChildrenData(String id) {
		return this.getBaseMapper().findChildrenData(id);
	}



	/**
	 * 根据父id获取子数据
	 * @param parentId
	 * @param allList
	 */
	private List<DicEntity>  findChildrenList(String parentId, List<DicEntity> allList) {
		return allList.stream().filter(o -> o.getPid().equals(parentId)).collect(Collectors.toList());
	}

	/**
	 * 递归遍历子节点数据
	 * @param childrenList
	 * @return
	 */
	private List<DicEntity> getChildrenList(List<DicEntity> childrenList) {
		childrenList.stream().forEach(x->{
			//父id
			String parentId = x.getId();
			//子数据
			List<DicEntity> childrenLists = this.list(new LambdaQueryWrapper<>(DicEntity.class).eq(DicEntity::getPid, parentId));
			x.setChildren(childrenLists);
			getChildrenList(childrenLists);
		});
		return childrenList;
	}


	/**
	 * 递归遍历子节点数据
	 * @param childrenList
	 * @return
	 */
	private List<DicEntity> getChildrenList(List<DicEntity> childrenList,List<DicEntity> allList) {
		childrenList.stream().forEach(x->{
			//父id
			String parentId = x.getId();
			//子数据
			//List<DicEntity> childrenLists = this.list(new LambdaQueryWrapper<>(DicEntity.class).eq(DicEntity::getPid, parentId));
			List<DicEntity> childrenLists = findChildrenList(parentId, allList);
			x.setChildren(childrenLists);
			//getChildrenList(childrenLists);
			getChildrenList(childrenLists,allList);
		});
		return childrenList;
	}


}
